import { specsList,specscount } from "../../request/api";

const state={
    list:[],
    size:2,
    page:1,
    total:0
}
const mutations ={
    changeList(state,arr){
        state.list = arr
        return arr
    },
    changePage(state,page){
        state.page = page
        return page
    },
    changeTotal(state,total){
        state.total = total
        return total
    },
    changeSize(state,size){
        state.size = size
        return size
    }
}
const actions = {
    asyncList(context){
        specsList({size:context.state.size,page:context.state.page}).then(res=>{
            let list = res.data.list?res.data.list:[]
            if(list.length ==0 && context.state.page >1 ){
                context.commit("changePage",context.state.page-1)
                context.dispatch("asyncList")
            }
            list.forEach(item=>{
                item.attrs = JSON.parse(item.attrs)
            })
            context.commit('changeList',list)
        })
    },
    asyncTotal(context){
        specscount().then(res=>{
          context.commit("changeTotal",res.data.list[0].total)
        })
      },
    asyncPage(context,page){
        context.commit("changePage",page)
        context.dispatch("asyncList")
    },

}
const getters = {
    list(state){
        return state.list
    },
    size(state){
        return state.size
    },
    total(state){
        return state.total
    }
}

export default{
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}